
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.GiftBag;
import com.kugou.game.sdk.entity.GiftbagReceiveResponse;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:礼包领取http处理
 * 
 * @author allenli
 * @since 2014-8-15 上午10:33:33
 */
public class GiftReceiveHttpTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public GiftbagReceiveResponse request(long giftId, String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        // 填充参数，key-value。key是接口要求传的变量名称
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GIFT_RECEIVE);
        params.put("giftid", giftId);
        params.put("username", userName);
        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        requestPackage.setUserName(userName);
        // 生成响应包
        ResponsePackage responsePackage = new GiftResponsePackage();
        // 生成响应类
        GiftbagReceiveResponse appsHttpResponse = new GiftbagReceiveResponse();
        requestParams.add(params);
        requestPackage.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_GIFT_RECEIVE);
        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    class GiftResponsePackage extends HttpBaseResponsePackage<GiftbagReceiveResponse> {

        @Override
        protected void handleResponse(GiftbagReceiveResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "GiftReceiveHttpTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_GIFT_RECEIVE.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.getJSONObject("data");
                        if (jsonResult != null) {
                            GiftBag gift = new GiftBag();
                            gift.setExtstatus(jsonResult.getInt("gift_state"));
                            gift.setExistscardnumber(jsonResult.getString("cardnum"));
                            // 领取状态 1代表领取成功
                            response.setStatus(jsonResult.getInt("status"));
                            response.setGift(gift);
                            response.setOk(true);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
